#include "music.h"


struct kernel_list *INIT_kernel_list()			//内核链表初始化
{
	struct kernel_list *p;
	p = (struct kernel_list *)malloc(sizeof(struct kernel_list));
	if(p == NULL )
		return NULL;
	bzero(p->name,256);
	INIT_LIST_HEAD(&p->list);
	
	return p;
}

void find_dir_mp3(char *path,struct kernel_list *head)			//查找一个路径下所有的.mp3格式文件
{
	printf("path = %s\n",path);
	char tmp[256];
	DIR *dir;
	struct dirent *fdir;
	bzero(tmp,256);
	dir = opendir(path);
	if(dir == NULL)
	{
		perror("open dir failed!\n");
		return;
	}
	
	while((fdir = readdir(dir)) != NULL)
	{
		
		if(strstr(fdir->d_name,".mp3") != NULL)
		{
			bzero(tmp,256);
			printf("---------------\n");
			struct kernel_list *new = INIT_kernel_list();
			printf("bmp_name = %s \n",fdir->d_name);
			printf("lei xing = %u\n",fdir->d_type);
			strcat(tmp,"madplay ");
			strcat(tmp,path);
			strcat(tmp,fdir->d_name);
			strcat(tmp," &");
			printf("%s\n",tmp);
			strcpy(new->name,tmp);
			list_add_tail(&new->list,&head->list);
			printf("insert success===========\n");
		}
		if(fdir->d_type == 4 && strcmp(fdir->d_name,".") != 0 && strcmp(fdir->d_name,"..") != 0)
		{	
			bzero(tmp,256);
			strcat(tmp,path);
			strcat(tmp,fdir->d_name);
			strcat(tmp,"/");
			printf("tmp = %s\n",tmp);
			find_dir_mp3(tmp,head);
		}	
	}
}

void find_dir_format(char *path,struct kernel_list *head,char *format)			//查找一个路径下所有的format格式文件
{
	printf("path = %s\n",path);
	char tmp[256];
	DIR *dir;
	struct dirent *fdir;
	bzero(tmp,256);
	dir = opendir(path);
	if(dir == NULL)
	{
		perror("open dir failed!\n");
		return;
	}
	
	while((fdir = readdir(dir)) != NULL)
	{		
		if(strstr(fdir->d_name,format) != NULL)
		{
			bzero(tmp,256);
			printf("---------------\n");
			struct kernel_list *new = INIT_kernel_list();
			strcat(tmp,path);
			strcat(tmp,fdir->d_name);
			printf("bmp_path = %s\n",tmp);
			strcpy(new->name,tmp);
			list_add_tail(&new->list,&head->list);		
		}
	}
}

char * current_music(char *bmp_name)
{
	int i = 0;
	char *music_name = malloc(256);
	bzero(music_name,256);
	char *p = bmp_name;
	music_name[i] = *p;
	i++;
	p++;
	while(*p != '.')
	{
		music_name[i] = *p;
		p++;
		i++;
	}
	strcat(music_name, ".mp3");

	return music_name;
}

char *current_picture(char *bmp_name)
{
	int i = 0;
	char *name_temp = malloc(256);
	char picture_name[256];
	bzero(picture_name,256);
	bzero(name_temp,256);
	char *p = bmp_name;
	while(*p != '/')
	{
		p++;
	}
	p++;
	while(*p != '/')
	{
		p++;
	}
	p++;
	while(*p != '\0')
	{
		picture_name[i] = *p;
		p++;
		i++;
	}
	sprintf(name_temp,"./music/musicname/%s",picture_name);
	return name_temp;
}


void play_music(char *music_name)
{
	char cmd_buf[256]={0};
	sprintf(cmd_buf,"madplay %s & \n",music_name);
	system(cmd_buf);
}

void stop_music(void)
{
	system("killall madplay \n");
}

void press_pause_music(void)
{
	system("killall -STOP madplay &");		
}

void continue_music(void)
{	
	system("killall -CONT madplay &");		
}
void *picture_thread(void *arg)
{
	int i = 0;
	while(1)
	{
		if(!press_pause)
		{
			lcd_draw_jpg_circle(385,240,100,(char *)arg,NULL,0,1,-i);
			i++;
			usleep(10*100);
		}
		
	}
}

void show_picture_list(struct kernel_list *p)
{
	struct list_head *q_pre;
	struct list_head *q_next;
	char name_buf[256]={0};
	
	lcd_draw_jpg_circle(385,240,100,p->name,NULL,0,1,0);//当前音乐的图片
	
	strcpy(name_buf,current_picture(p->name));
	lcd_draw_jpg(200,50,name_buf,NULL,0,0);
	
	
	q_pre = p->list.prev;
	if(!(*(list_entry(q_pre,struct kernel_list,list)->name)))
		q_pre = q_pre->prev;
	//lcd_draw_jpg_circle(215,240,50,list_entry(q_pre,struct kernel_list,list)->name,NULL,0,1,0);

	q_next = p->list.next;
	if(!(*(list_entry(q_next,struct kernel_list,list)->name)))
						q_next = q_next->next;
	//lcd_draw_jpg_circle(555,240,50,list_entry(q_next,struct kernel_list,list)->name,NULL,0,1,0)	;
}
int slide(int x1, int y1, int x2, int y2)
{
	float tan;
	tan = (float)(y2-y1)/(x2-x1);
	if(  tan<1 && tan>(-1) && x2>x1 )
		return 1;	//右划
	else if (  tan<1 && tan>(-1) && x2<x1 )
		return 2;	//左划
	else 
		return 0;
}



void music_album()
{
	pthread_t pid;
	struct coordinate coor_first, coor_second;
	bool released = false;
	bool start = false;
	press_pause = false;
	char music_path[256];
	bzero(music_path, 256);
	struct kernel_list *picture_head;//保存图片
	struct kernel_list *p_picture;
	struct list_head *q_picture;
	picture_head = INIT_kernel_list();
	find_dir_format(MUSIC_PATH,picture_head,".jpg");
	
	q_picture = picture_head->list.next;
	p_picture = list_entry(q_picture,struct kernel_list,list);
	lcd_draw_jpg(0,0,MUSIC_BACKGROUND,NULL,0,0);	
	show_picture_list(p_picture);
	
	
	while(1)
	{
		released = true;
		ts_trace(ts_fd, &coor_first, &released);
		if(coor_first.y>0 && coor_first.y<380 &&!released)
		{
			usleep(100*1000);
			
			ts_trace(ts_fd, &coor_second, &released);
			if(!released)
			{
				if(slide(coor_first.x,coor_first.y,coor_second.x, coor_second.y) == 1)//右划
				{
					if(start)
					{
						pthread_cancel(pid);
						start = false;
						stop_music();
						lcd_draw_jpg(350,400,MUSIC_STOP,NULL,0,0);
					}
					q_picture = q_picture->prev;
					if(!(*(list_entry(q_picture,struct kernel_list,list)->name)))
						q_picture = q_picture->prev;	
					p_picture = list_entry(q_picture,struct kernel_list,list);
					show_picture_list(p_picture);	
				}
				
				if(slide(coor_first.x,coor_first.y,coor_second.x, coor_second.y) == 2)//左划
				{
					if(start)
					{
						pthread_cancel(pid);
						start = false;
						stop_music();
						lcd_draw_jpg(350,400,MUSIC_STOP,NULL,0,0);
					}
					q_picture = q_picture->next;
					if(!(*(list_entry(q_picture,struct kernel_list,list)->name)))
						q_picture = q_picture->next;	
					p_picture = list_entry(q_picture,struct kernel_list,list);
					show_picture_list(p_picture);	
				}
				while(1)
				{
					ts_trace(ts_fd, &coor_second, &released);
					if(released)
						break;
				}
			}
		}
		else if(coor_first.y>400 && coor_first.y<470 && coor_first.x>350 && coor_first.x<420 &&!released)//切换播放状态，继续或暂停
		{
		
			if(!start)
			{
				strcpy(music_path ,current_music(list_entry(q_picture,struct kernel_list,list)->name));
				play_music(music_path);
				pthread_create(&pid ,NULL,picture_thread, (void *)list_entry(q_picture,struct kernel_list,list)->name);
				start = true;
				lcd_draw_jpg(350,400,MUSIC_START,NULL,0,0);
			}
			else if(start && !press_pause)
			{
				press_pause_music();
				press_pause = true;
				lcd_draw_jpg(350,400,MUSIC_STOP,NULL,0,0);
			}
			else if(start && press_pause)
			{
				continue_music();
				press_pause = false;
				lcd_draw_jpg(350,400,MUSIC_START,NULL,0,0);
			}		
		}
		else if(coor_first.y>410 && coor_first.y<450 && coor_first.x>270 && coor_first.x<320 &&!released)//上一首
		{
			if(start)
			{
				pthread_cancel(pid);
				start = false;
				stop_music();
				lcd_draw_jpg(350,400,MUSIC_STOP,NULL,0,0);
			}	
			q_picture = q_picture->prev;
			if(!(*(list_entry(q_picture,struct kernel_list,list)->name)))
				q_picture = q_picture->prev;	
			p_picture = list_entry(q_picture,struct kernel_list,list);
			show_picture_list(p_picture);		
		}
		else if(coor_first.y>410 && coor_first.y<450 && coor_first.x>450 && coor_first.x<500 &&!released)//下一首
		{
			if(start)
			{
				pthread_cancel(pid);
				start = false;
				stop_music();
				lcd_draw_jpg(350,400,MUSIC_STOP,NULL,0,0);
				
			}	
			q_picture = q_picture->next;
			if(!(*(list_entry(q_picture,struct kernel_list,list)->name)))
				q_picture = q_picture->next;	
			p_picture = list_entry(q_picture,struct kernel_list,list);
			show_picture_list(p_picture);		
		}	
		else if(coor_first.y>410 && coor_first.y<450 && coor_first.x>530 && coor_first.x<580 && released)//退出
		{
			printf("exit!\n");
			if(start)
			{
				
				pthread_cancel(pid);
				stop_music();
			}
			return;		
		}	
	} 	
}






















